package com.ruoyi.bearing.controller;

import com.ruoyi.bearing.domain.BearingModel;
import com.ruoyi.bearing.domain.BearingPredictResult;
import com.ruoyi.bearing.domain.ModelInfoMangment;
import com.ruoyi.bearing.domain.OptimizedModel;
import com.ruoyi.bearing.service.IBearingPredictionService;
import com.ruoyi.bearing.service.IModelInfoMangmentService;
import com.ruoyi.bearing.service.IOptimizedModelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.flask.FlaskException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 在线预测
 */
@Api("在线在线预测")
@RestController
@RequestMapping("/predict")
public class BearingPredictionController extends BaseController {
    @Autowired
    private IBearingPredictionService iBearingPredictionService;
    @Autowired
    private IOptimizedModelService iOptimizedModelService;
    @Autowired
    private IModelInfoMangmentService iModelInfoMangmentService;

    /**
     * 在线预测
     */
    @ApiOperation("在线预测")
    @PreAuthorize("@ss.hasPermi('predict:list')")
    @PostMapping("/list")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult list(@RequestParam("files") MultipartFile[] files, BearingModel bearingModel) throws IOException, IllegalAccessException {
        startPage();
        List<OptimizedModel> optimizedModelList = iOptimizedModelService.selectOptimizedModelList(new OptimizedModel());
        if (optimizedModelList.isEmpty()) {
            throw new BaseException("请设置算法模型");
        }
        if (1 == optimizedModelList.size() & optimizedModelList.get(0).getRulModel() == null) {
            throw new BaseException("请设置预测模型");
        }
        // String mid = optimizedModelList.get(0).getRulModel();
        // ModelInfoMangment modelInfoMangment = iModelInfoMangmentService.selectModelInfoMangmentByMid(mid);
        // bearingModel.setRulModel(mid);
        // bearingModel.setRulType(modelInfoMangment.getType());
        BearingPredictResult bearingPredictResult = iBearingPredictionService.startBearingPrediction(files[0], bearingModel);
        if (null == bearingPredictResult) {
            throw new FlaskException();
        }
        return success(bearingPredictResult);
    }

    /**
     * 下载在线预测结果
     */
    @ApiOperation("下载在线预测结果")
    @PreAuthorize("@ss.hasPermi('predict:download')")
    @PostMapping("/download")
    public void download(HttpServletResponse response, BearingModel bearingModel) {
        iBearingPredictionService.download(response, bearingModel);
    }
}
